<!--
 * @Description: 积分审批 - 审批意见
 * @Author: binbin binbin@qq.com
 * @Date: 2024-03-05
 * @LastEditors: Bart 1714877505@qq.com
 * @LastEditTime: 2025-03-24
-->
<template>
  <div class="edit">
    <a-modal v-model:visible="visible" :title="type === 1 ? '同意' : '拒绝'" @ok="onOk">
      <a-form :label-col="{ span: 6 }" :wrapper-col="{ span: 16 }">
        <a-form-item label="审批意见" v-bind="validateInfos.remark">
          <a-textarea :rows="4" v-model:value="form.state.remark" placeholder="请输入审批意见" allow-clear :maxlength="150" />
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>

<script lang="ts" setup>
  import { Form, message } from 'ant-design-vue'
  import { computed, reactive, watch } from 'vue'

  import { requestApproveTask, requestRejectTask } from '/@/api/integral/integralApprove'
  import { HttpCodeEnum } from '/@/enums/httpEnum'

  const props = defineProps({
    visible: { type: Boolean, default: false },
    taskId: { type: String, default: () => '' },
    auditId: { type: String, default: () => '' },
    type: { type: Number, default: 1 },
  })

  const emits = defineEmits(['update:visible', 'success'])

  const visible = computed({
    get: () => props.visible,
    set: (val: boolean) => emits('update:visible', val),
  })

  /** 表单配置 */
  const form = reactive({
    state: {
      remark: '',
    },
    rule: {
      remark: [{ required: true, message: '请输入审批意见' }],
    },
  })
  /** 表单hook */
  const { validate, validateInfos, resetFields } = Form.useForm(form.state, form.rule)

  watch(
    () => visible.value,
    (newVal, oldVal) => {
      if (newVal && !oldVal) resetFields()
    },
  )

  /**
   * 确定
   */
  const onOk = () => {
    validate()
      .then(async () => {
        if (props.type === 1) {
          handleApprove()
        } else {
          handleReject()
        }
      })
      .catch(() => {})
  }

  const handleApprove = async () => {
    const { remark } = form.state
    const { responseCode } = await requestApproveTask({
      taskId: props.taskId,
      businussId: props.auditId,
      formList: [
        {
          formId: '1',
          fieldKey: 'approvalOpinions',
          fieldValue: remark,
        },
      ],
    })
    if (responseCode === HttpCodeEnum.SUCCESS) {
      message.success('操作成功')
      visible.value = false
      emits('success')
    }
  }
  const handleReject = async () => {
    const { remark } = form.state
    const { responseCode } = await requestRejectTask({
      taskId: props.taskId,
      businussId: props.auditId,
      formList: [
        {
          formId: '1',
          fieldKey: 'approvalOpinions',
          fieldValue: remark,
        },
      ],
    })
    if (responseCode === HttpCodeEnum.SUCCESS) {
      message.success('操作成功')
      visible.value = false
      emits('success')
    }
  }
</script>

<style lang="less" scoped></style>
